/**
 * 神州数码 (2011)
 */
package net.abbp.mtr.config;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 通道配置
 * 
 * @version 1.0 2011-3-6
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ABBP 1.0
 */
public enum ChannelManager {
	INSTANCE;

	// 日志记录器
	private Logger logger = Logger.getLogger(ChannelManager.class.getName());
	// 配置信息
	private Map<String, ChannelConfig> configs = new HashMap<String, ChannelConfig>();

	// 初始化，读取配置文件
	ChannelManager() {
		String cfgFile = System.getProperty("user.dir") + File.separator + "channel.xml";

		if (logger.isDebugEnabled()) {
			logger.debug("读取通道信息，使用配置文件：" + cfgFile);
		}

		try {
			SAXReader reader = new SAXReader();
			Document document = reader.read(new File(cfgFile));

			Element root = document.getRootElement();
			Element channels = root.element("channels");
			for (Object o : channels.elements("channel")) {
				Element ele = (Element) o;

				String id = ele.attributeValue("id");
				ChannelConfig cc = new ChannelConfig(id, ele.attributeValue("caption"));
				this.configs.put(id, cc);
			}
		} catch (DocumentException e) {
			logger.error(e);
		}
	}

	/**
	 * 通过通道Id获取通道配置信息描述
	 * 
	 * @param channelId
	 *            通道Id
	 * @return 通道配置信息描述
	 */
	public String getChannelConfig(String channelId) {
		ChannelConfig cc = this.configs.get(channelId);
		if (cc == null) {
			return "无信息";
		} else {
			return cc.caption;
		}
	}

	/**
	 * 获取配置信息
	 * 
	 * @return
	 */
	public Map<String, ChannelConfig> getConfigs() {
		return this.configs;
	}
}
